#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/1/30 16:09
# @Author  : 王凯
# @File    : useragent_random.py
# @Project : scrapy_spider
from faker import Faker
from scrapy.downloadermiddlewares.useragent import UserAgentMiddleware


class RandomUserAgentMiddleware(UserAgentMiddleware):
    def spider_opened(self, spider):
        self.user_agent = Faker().user_agent()

    def process_request(self, request, spider):
        request.headers.setdefault(b"User-Agent", Faker().user_agent())
